package com.yanzhu.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 随机数、随即字符串工具
 *
 * Created by Hyman on 2017/2/28.
 */
public class RandomUtils {
    public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String numberChar = "0123456789";

    /**
     * 返回一个定长的随机字符串(只包含大小写字母、数字)
     *
     * @param length 随机字符串长度
     * @return 随机字符串
     */
    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(allChar.charAt(random.nextInt(allChar.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机纯字母字符串(只包含大小写字母)
     *
     * @param length 随机字符串长度
     * @return 随机字符串
     */
    public static String generateMixString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(allChar.charAt(random.nextInt(letterChar.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机纯小写字母字符串(只包含小写字母)
     *
     * @param length 随机字符串长度
     * @return 随机字符串
     */
    public static String generateLowerString(int length) {
        return generateMixString(length).toLowerCase();
    }

    /**
     * 返回一个定长的随机纯大写字母字符串(只包含大写字母)
     *
     * @param length 随机字符串长度
     * @return 随机字符串
     */
    public static String generateUpperString(int length) {
        return generateMixString(length).toUpperCase();
    }

    /**
     * @param moneySum 输入总金额
     * @param redNum 输入红包数量
     */
    public static List<String> wxAlgorithm(double moneySum, int redNum) {
        List<String> list = new ArrayList<>();
        // 设置最小的金额
        double moneyMin = 0.01;
        Random random = new Random();
        //精确小数点2位
        NumberFormat formatter = new DecimalFormat("#.##");
        for (int i=1;i<redNum;i++)
        {
            //随机一个数，数值范围在最小值与余额之间
            String money = formatter.format(random.nextDouble() * (moneySum - moneyMin) + moneyMin);
            list.add(money);
            //数值转换
            moneySum = Double.valueOf(formatter.format(moneySum - Double.valueOf(money)));

            System.out.println("第"+i+"个红包：" + money + "元 ,余额：" + moneySum);
        }

        System.out.println("最后个红包：" + moneySum + "元 ,余额：" + (moneySum - moneySum));
        list.add(moneySum + "");
        return list;

    }

//    public static List<String>  wxAlgorithm(double total,int num,double min){
//        for(int i=1;i<num;i++){
//            double safe_total=(total-(num-i)*min)/(num-i);
//            double money=Math.random()*(safe_total-min)+min;
//            BigDecimal money_bd=new BigDecimal(money);
//            money=money_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//            total=total-money;
//            BigDecimal total_bd=new BigDecimal(total);
//            total=total_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//            System.out.println("第"+i+"个红包："+money+",余额为:"+total+"元");
//        }
//        System.out.println("第"+num+"个红包："+total+",余额为:0元");
//    }

    public static void main(String[] args) {
//        System.out.println(generateString(32));
//        System.out.println(generateMixString(32));
//        System.out.println(generateLowerString(32));
//        System.out.println(generateUpperString(32));

        wxAlgorithm(10.26, 9);
    }

}